home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 935 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Constructor Exceptions
  5. Date: 8 Jan 1996 15:18:32 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4crck8$9oh@dawn.mmm.com>
  8. References: <4bud9g$pv5@oxy.rust.net> <4cbhcl$kst@dawn.mmm.com> <4ce68n$8u4@sundog.tiac.net> <4cglh3$6ue@oxy.rust.net>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Paul Gunn (pgunn@mail.cbf.com) wrote:
  13. > page@tiac.net (Chris Page) wrote:
  14.  
  15.  
  16. > >Sure, they'll clean up 'this', but the programmer is responsible for
  17. > >any and all heap memory allocated within the body of constructor.
  18.  
  19. > My impression was that they did not. I'll try to find the article I
  20. > read -- perhaps I misunderstood the point.
  21.  
  22. > >The solution to this problem is to use the "resource allocation is
  23. > >class initialization" technique, which has been discussed in the news
  24. > >group many time before.
  25.  
  26. > Could you give a brief synopsis of this technique? I may be familiar
  27. > with it, but not by name.
  28.  
  29. This term is used to describe classes which acquire resources in their
  30. constructors and release them in their destructors.  (More loosely
  31. applied, objects of those kinds of classes may not actually acquire
  32. the resources, but may be used to hold on to them for release in their
  33. destructors.)
  34.  
  35. One example is the ofstream class.  Given a name, it opens the file.
  36. When the ofstream is destroyed, it will close the file.
  37.  
  38. Another example of this is auto_ptr<T>, which holds a pointer to a
  39. dynamically allocated T.  The destructor of auto_ptr<T> deletes the
  40. pointer.  This is useful for holding onto memory that is no longer
  41. needed outside a certain block.  You can give the pointer to an
  42. auto_ptr<T> and rest assured that the memory will be released no matter
  43. how you exit the block.
  44.  
  45. See also section 16.5 of "The Design and Evolution of C++," by Bjarne
  46. Stroustrup.
  47. --
  48. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  49. 3M Company                      phone:  (612) 737-4643
  50. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  51. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  52.     But 3M speaks for me -- I did not write the following line:
  53.  
  54. Opinions expressed herein are my own and may not represent those of 3M.
  55.